conn = new WebSocket(
		'ws://localhost:8080/zlaten vek index/zlatniq-vek/bin/server.php');
conn.onopen = function(e) {
	console.log("Connection established!!!");
};

conn.onmessage = function(e) {
	var msg = e.data;
	var objMsg = $.parseJSON(msg);
	console.log(objMsg);
	if (objMsg.type == "resourceId") {
		console.log(objMsg.value);
		$.post("index.php?page=home", {
			action : "checkResourceId",
			resourceId : objMsg.value
		});
		$.post("index.php?page=home", {
			action : "getUserId"
		}, function(data, status) {
			conn.send('{"type": "userId", "id": "' + data + '"}');
		});
	} else if (objMsg.type == "teritory") {
		var t = objMsg.area;
		console.log(t);
		var path = Risk.Territories[t].path;
		var group = new Kinetic.Group();

		//We have to set up a group for proper mouseover on territories and sprite name images 
		group.add(path);
		Risk.mapLayer.add(group);
		Risk.Territories[t].color = objMsg.color;

		Risk.Territories[t].path.setFill(Risk.Settings.colors[Risk.Territories[t].color]);
		Risk.Territories[t].path.setOpacity(0.8);
		group.moveTo(Risk.mapLayer);
		Risk.topLayer.draw();
		Risk.mapLayer.draw();
	} else if (objMsg.type == "question") {
		$("#notice").hide(1000);
		showQuestion(objMsg);
		//console.log("vlizam tuka");
	} else if (objMsg.type == "confirmGame") {
			$("#confirmMessage").html("Започване на игра сега");
			$("#confirmGame").show(1000);
		
	} else if (objMsg.type == "renderGame") {
		$("#notice").hide();
		$.post("index.php?page=game", {
			action : "renderGame"
		}, function(data, status) {
			$("#wrapper").html(data);
		});
	} else if (objMsg.type == "selectTeritory") {
		$("#notice").hide(1000);
	} else if (objMsg.type == "waitingSelecting") {
		console.log("waiting selecting");		
		$("#notice").show(1000);
		$("#waitMessage").html("Моля изчакайте противникът да избере територия!");
	} else if (objMsg.type == "correctAnswer") {
		console.log("message received");
		$("#waitMessage").html(objMsg.isCorrect + " отговор! <p>Верният отговор е: " + objMsg.correct + "</p>");
		$("#notice").show(1000);
		setTimeout(function() {
			$("#notice").hide(1000);
			conn.send('{"type": "getAction"}');
		}, 4000);
	} else if (objMsg.type == "broadcastChat") {
		$("#chatbox").append("<p class=\"msg\">" + objMsg.msg + "</p>");
		$('#chatbox').scrollTop(1000000);
	}
};


//da se implementira logikata da se pokazva saobshtenie chata v igrata 
